<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>好分区的数目</title>
</head>
<body>
  <script>
    function fn(nums, k) {
      if (nums.reduce((t, c) => t + c, 0) < 2 * k) return 0;
      const MOD = 10 ** 9 + 7;
      const n = nums.length;
      const f = new Array(n + 1).fill(0).map(() => new Array(k).fill(0));
      f[0][0] = 1;
      let total = 1;
      for (let i = 1; i <= n; i++) {
        total = total * 2 % MOD;
        const x = nums[i - 1];
        for (let j = 0; j < k; j++) {
          f[i][j] = f[i - 1][j];
          if (j >= x) {
            f[i][j] = (f[i][j] + f[i - 1][j - x]) % MOD;
          }
        }
      }
      return (total + MOD - 2 * f[n].reduce((t, c) => (t + c) % MOD, 0)) % MOD
    }
    console.log(fn([1, 2, 3, 4], 4))
  </script>
</body>
</html>